package com.mao.action.adminmanage;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.mao.action.BaseAction;
import com.mao.action.jsonvo.store.JsonVoStore;
import com.mao.action.jsonvo.store.JsonVoUser;
import com.mao.model.store.Store;
import com.mao.model.user.User;
import com.mao.service.adminmanage.IAdminManageService;
import com.mao.util.MD5Util;

@SuppressWarnings("unchecked")
public class StoreUserManage extends BaseAction {

	private IAdminManageService adminManageService;

	private Map result;

	private Store store;

	private User user;

	private Long storeid = new Long(0);

	private Long id;

	private String name;
	
	// Json获取所有商家
	public String getALlStore() {
		result = new HashMap();
		List<JsonVoStore> list = new ArrayList<JsonVoStore>();
		list = adminManageService.getAllStore(name);
		result.put("total", adminManageService.getAllStoreCount(name));
		result.put("rows", list);
		return SUCCESS;
	}

	// 跳到商家管理页面
	public String toStore() {
		return SUCCESS;
	}

	public String addOrUpdateStore() {
		store.setCreateTime(new Date());
		adminManageService.saveOrUpdateStore(store);
		return SUCCESS;
	}

	// 跳到用户管理页面
	public String toUser() {
		return SUCCESS;
	}

	// 获取用户列表
	public String getAllUser() {
		result = new HashMap();
		List<JsonVoUser> list = adminManageService.getAllUser(name);
		result.put("total", adminManageService.getAllUserCount(name));
		result.put("rows", list);
		return SUCCESS;
	}

	public String addOrUpdateUser() {
		Store store = adminManageService.getStoreById(storeid);
		// 修改用户
		if (user.getId() != null) {
			User updateuser = adminManageService.getUserById(user.getId());
			updateuser.setLoginName(user.getLoginName());
			updateuser.setName(user.getName());
			updateuser.setIsAdmin(user.getIsAdmin());
			updateuser.setPhone(user.getPhone());
			updateuser.setAddress(user.getAddress());
			updateuser.setNote(user.getNote());
			updateuser.setStore(store);
			if (!updateuser.getPassword().equals(user.getPassword())) {
				updateuser.setPassword(MD5Util.MD5(user.getPassword()));
			}
			adminManageService.saveOrUpdateUser(updateuser);
			user = updateuser;
		} else { // 新增用户
			user.setStore(store);
			user.setPassword(MD5Util.MD5(user.getPassword()));
			if (user.getId() == null) {
				user.setCreateTime(new Date());
			}
			adminManageService.saveOrUpdateUser(user);
		}
		return SUCCESS;
	}

	public String delStore() {
		store = adminManageService.getStoreById(storeid);
		store.setStatus(Store.STATUS_DELETED);
		adminManageService.saveOrUpdateStore(store);
		return SUCCESS;
	}

	public String delUser() {
		user = adminManageService.getUserById(id);
		user.setStatus(User.STATUS_DELETED);
		adminManageService.saveOrUpdateUser(user);
		return SUCCESS;
	}
	
	public String getUserByStore() {
		result = new HashMap();
		List<JsonVoUser> list = adminManageService.getUserByStore(storeid);
		result.put("total", adminManageService.getUserByStoreCount(storeid));
		result.put("rows", list);
		return SUCCESS;
	}

	public IAdminManageService getAdminManageService() {
		return adminManageService;
	}

	public void setAdminManageService(IAdminManageService adminManageService) {
		this.adminManageService = adminManageService;
	}

	public Map getResult() {
		return result;
	}

	public void setResult(Map result) {
		this.result = result;
	}

	public Store getStore() {
		return store;
	}

	public void setStore(Store store) {
		this.store = store;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public Long getStoreid() {
		return storeid;
	}

	public void setStoreid(Long storeid) {
		this.storeid = storeid;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

}
